package edu.sjsu.cs.queryopt.dbms.main;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Helper {
	/**
	 * Checking if there is right colname
	 * for example we can't use "*" with col's name so
	 * if there is "*" with col name , we return error.
	 *  
	 *  @param colname: arraylist that contain cols
	 * **/
	public  boolean IsTableNameAndStarTogether(ArrayList<String> colname)
	{
		if(colname.size() == 1 && colname.get(0).charAt(0) == '*')
		{// if there is only * then fine
			return true;
		}
		else if(colname.size() > 1 && !colname.contains("*"))
		{// if there is only col then fine
			return true;
		}
		else{
			return false;
		}
	}
	//
	public boolean IsTableNameCorrectList(ArrayList<String> tableName)
	{	
		
		for(int i = 0 ; i < tableName.size() ; i++)
		{
			if(tableName.get(i).charAt(0) == '*'){
				return true;
			}
			else if(tableName.get(i).charAt(0)  != 'T')
			{
				return false;
			}
		
		}
		return true;
		//return ((tableName.charAt(0)) == WordCapsule.QWC_PRE_TABLE);			
	}
	
	public static boolean IsTableNameCorrect(String tableName)
	{		
		return ((tableName.charAt(0)) == WordCapsule.QWC_PRE_TABLE);			
	}
	public static boolean IsIndexCorrect(String tableName)
	{	
//		System.out.println("tableName " + tableName);
//		System.out.println("TRUE ?? " +((tableName.charAt(0)) == WordCapsule.QWC_PRE_INDEX));
		return (Character.toString((tableName.charAt(0))).equals(WordCapsule.QWC_PRE_INDEX));			
	}
	
	public static int howManyCols(String [] order)
	{
		int comCounter = 0;
		for(int index = 0 ; index < order.length ; index++)
		{
			if(order[index].equals(WordCapsule.QWC_Com))
			{
				comCounter++;
				
			}
			
		}
//		System.out.println("We have " + (comCounter+1));
		return comCounter + 1;
		
	}
	public static boolean isParenthesesCorrcent(String[] query , int position)
	{
//			System.out.println("position " + position);
//			System.out.println("query[position+1] " + query[position+1]);
//			System.out.println("query[query.length-1] " + query[query.length-1]);
			if(query[position+1].equals(WordCapsule.QWC_L_prth) && query[query.length-1].equals(WordCapsule.QWC_R_prth))
			{
				return true;

			}
			else
			{
				System.out.println("Helper : isParenthesesCorrcent is not ( error");
				return false;
			}
		
	
		
	}
	public static String WriteBuildTableName(ArrayList<String> al)
	{
		String tablename = "";
	//	System.out.println("ReadBuildTableName" + al);
		tablename += al.get(1);
		tablename += al.get(0);
		//tablename += WordCapsule.resultfiletype;
	//	System.out.println("ReadBuildTableName " + tablename);
		return tablename;
		
		
	}
	public static String ReadBuildTableName(ArrayList<String> al)
	{
		String tablename = "";
		//System.out.println("ReadBuildTableName");
		tablename += al.get(1);
		tablename += WordCapsule.tablefiletype;
	//	System.out.println("ReadBuildTableName " + tablename);
		return tablename;
		
		
	}
//	public static boolean isIndexFormatCorrent(String[] query , int position, int numberofindex)
//	{
//		
//		
//	}
	
	public static int isACSorDSC(String index)
	{
		if(index.charAt(index.length()-1) == WordCapsule.ACS)
		{
			return 1;
		}
		else if(index.charAt(index.length()-1) == WordCapsule.DCS)
		{
			
			return 2;
		}
		else
		{
			return -1;
			
		}
		
	}
	public String ListToString(ArrayList<String> al){
		String re = "";
		for(int index = 0 ; index < al.size() ; index++){
			re+= al.get(index) + " ";
		}
//		System.out.println("ListToString " + re);
		return re;
		
	}
	public static String [] StringToArray(String sentance)
	{
		
		String[] tokens = sentance.split(" ");
		return tokens;
		
	}
	public static ArrayList<String> StringToArrayList(String st)
	{
		ArrayList<String> al = new ArrayList<String>();

		StringTokenizer stt = new StringTokenizer(st);
		while (stt.hasMoreTokens()) {
			 
			  al.add(stt.nextToken());
			} 
//		for(int i = 0 ; i < al.size() ; i++)
//		{
//			System.out.println("StringToArrayList " + i +" : "+ al.get(i));
//		}
//		
		return al;
		
	}
	public static int HowManyColInTable(String sentance)
	{
		String[] tokens = sentance.split(" ");
		return tokens.length;
	}
	public static ArrayList<String> GetDef(ArrayList<String> pod , int start , int end)
	{

		 ArrayList<String> rv = new  ArrayList<String>();
		 for(int index = start ; index <= end ; index ++)
		 {
			 	rv.add(pod.get(index));
		 }
		 return rv;
	}

	  public static void deletefile(String file){
		  File f1 = new File(file+".idx");
		  boolean success = f1.delete();
		  if (!success){
//		  System.out.println("Deletion failed.");
		  
		  }else{
//		  System.out.println("File deleted.");
		    }
		  }
	  public static void deletefile2(){
		  File f1 = new File("output.txt");
		  boolean success = f1.delete();
		  if (!success){
//		  System.out.println("Deletion failed.");
		  
		  }else{
//		  System.out.println("File deleted.");
		    }
		  }
	  public static Hashtable<String, String> BuildWordpod()
	  {
		Hashtable <String,String>ht = new Hashtable<String, String>();  
		String [] LowerFrontStart  = { "a", "b", "c", "d", "e", "f", "g",
				"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
				"u", "v", "w", "x", "y", "z" };
		int end = LowerFrontStart.length-1;
		for(int index = 0 ; index < LowerFrontStart.length ; index++)
		{
			ht.put(LowerFrontStart[index], LowerFrontStart[end--]);
		}
		end = LowerFrontStart.length-1;
		for(int i = 0 ; i < LowerFrontStart.length ; i++)
		{
			ht.put(LowerFrontStart[i].toUpperCase(), LowerFrontStart[end--].toUpperCase());
		}
		ht.put(" ",  " ");
		
		return ht;
	  }
	
}
